<?php
session_start();
if (isset($_SESSION["account"])) {
?>

<?php require_once('common/header.php'); ?>
<?php require_once('common/sidebar.php'); ?> 
        <div class="main-content app-content mt-0">
                <div class="side-app">
<!--占位-->
<div class="card"> </div>
<!--占位-->
                    <!-- CONTAINER -->
                    <div class="main-container container-fluid">
                        <!--这是获取海报文件-->
                       <?php 
                        include_once("../untils/conn.php");
                        mysqli_query($con, "set names utf8");
                        
                        // 获取poster_url数据
                        $sql = "SELECT poster_url FROM poster";
                        $result = mysqli_query($con, $sql);
                        
                        // 将poster_url存储在一个数组变量中
                        $poster_urls = array();
                        while ($row = mysqli_fetch_assoc($result)) {
                            $poster_urls[] = $row['poster_url'];
                        }
                        ?>
                        <!-- PAGE-HEADER -->
                        
                    <div class="row row-sm">
                        <div class="col-lg-12">
                            <div class="card">
                                <div class="card-header">
                                    <h3 class="card-title">商品列表</h3></h3>
                                    <a href="#" class="btn btn-info" style="margin-left:20px;" target="_blank" onclick="openShopLink()">号卡店铺</a>
                                </div>
                                <div class="card-body">
                                    <div class="table-responsive">
                                        <table class="table table-bordered text-nowrap border-bottom" id="basic-datatable">
                                            <thead>
                                                <tr>
                                                    <th class="wd-10p border-bottom-0">商品ID</th>
                                                    <th class="wd-10p border-bottom-0">商品主图</th>
                                                     <th class="wd-10p border-bottom-0">运营商</th>
                                                    <th class="wd-10p border-bottom-0">商品名称</th>
                                                    <th class="wd-10p border-bottom-0">介绍</th>
                                                    <th class="wd-10p border-bottom-0">商品佣金</th>
                                                    <th class="wd-10p border-bottom-0">归属地</th>
                                                    <th class="wd-10p border-bottom-0">商品状态</th>
                                                    <th class="wd-10p border-bottom-0">商品说明</th>
                                                    <th class="wd-10p border-bottom-0">操作</th>
                                                </tr>
                                            </thead>
                                            <tbody id="goods-body">
                                            </tbody> 
                                        </table>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                    <!-- CONTAINER CLOSED -->

                </div>
            </div>
             <!-- MODAL EFFECTS -->
             <div class="modal fade" id="modaldemo8">
                <div class="modal-dialog modal-dialog-centered text-center" role="document">
                    <div class="modal-content modal-content-demo">
                        <div class="modal-header">
                            <h6 class="modal-title">生成海报</h6>
                            <button aria-label="Close" class="btn-close" data-bs-dismiss="modal"><span aria-hidden="true">&times;</span></button>
                        </div>
                        <div class="modal-body">
                            <div id="poster-carousel" class="carousel slide" data-bs-ride="carousel">
                                <div class="carousel-inner">
                                    <?php foreach ($poster_urls as $index => $poster_url) { ?>
                                        <div class="carousel-item <?php if ($index == 0) { echo 'active'; } ?>">
                                            <div class="poster-image-container" style="position: relative;">
                                                <img class="poster-image" src="<?php echo $poster_url; ?>" alt="Poster <?php echo ($index + 1); ?>">
                                                <img class="qr-code-image" src="" alt="QR Code" style="position: absolute; bottom: 20; right: 20; width: 100px; height: 100px;">
                                            </div>
                                        </div>
                                    <?php } ?>
                                </div>
            
                                <a class="carousel-control-prev" href="#poster-carousel" role="button" data-bs-slide="prev">
                                    <span class="carousel-control-prev-icon" aria-hidden="true"></span>
                                    <span class="visually-hidden">上一张</span>
                                </a>
                                <a class="carousel-control-next" href="#poster-carousel" role="button" data-bs-slide="next">
                                    <span class="carousel-control-next-icon" aria-hidden="true"></span>
                                    <span class="visually-hidden">下一张</span>
                                </a>
                            </div>
                        </div>
                        <div class="modal-footer">
                            <button class="btn btn-primary" onclick="savePoster()">保存本地</button>
                            <button class="btn btn-light" data-bs-dismiss="modal">取消</button>
                        </div>
                    </div>
                </div>
            </div>

            
             <?php
        session_start();
        include_once("../untils/conn.php");
        mysqli_query($con, "set names utf8");
        
        // 获取当前正在登录的账号信息的代理
        $proxy_acc = $_SESSION["account"];
        
        // 查询proxy表中的数据
        $query = "SELECT proxy_rename_sta FROM proxy WHERE proxy_acc = '$proxy_acc'";
        $result = mysqli_query($con, $query);
        $row = mysqli_fetch_assoc($result);
        
        if ($row) {
            $proxy_rename_sta = $row['proxy_rename_sta'];
        ?>
        
        <div class="modal fade" id="modaldemo5" data-bs-backdrop="static" data-bs-keyboard="false">
            <div class="modal-dialog modal-dialog-centered text-center" role="document">
                <div class="modal-content tx-size-sm">
                    <div class="modal-body text-center p-4 pb-5">
                        <i class="icon icon-close fs-70 text-danger lh-1 my-5 d-inline-block"></i>
                        <h4 class="text-danger">警告: 您的账号未实名！</h4>
                        <p class="mg-b-20 mg-x-20">平台要求所有代理商必须实名！否则无法使用平台功能！</p>
                        <a href="profile.php" class="btn btn-primary pd-x-25">去实名</a>
                    </div>
                </div>
            </div>
        </div>
        
        
            <!--/div-->
            
<script src="assets/js/jquery.min.js"></script>

   <script>
    var puid = ''; // 定义全局变量puid

$.ajax({
    url: "controller/public/GetPuid.php",
    type: "POST",
    dataType: "json",
    success: function(res) {
        // 获取成功后的代码
        puid = res.puid; // 将获取到的puid保存到全局变量中
    },
    error: function() {
        // 获取失败后的代码
        alert("获取puid失败");
    }
});

$.ajax({
    url: "controller/Goods.php",
    type: "POST",
    dataType: "json",
    success: function(data) {
        // 获取成功后的代码
        var protocol = location.protocol; // 获取当前页面的协议
        var hostname = location.hostname; // 获取当前页面的主机名
        for (var i = 0; i < data.length; i++) {
            var item = data[i];
            var state = "";
            var button = "";
            if (item.state == 0) {
                state = "上架中";
                button = '<a href="controller/Proxy_upstate.php?id=' + item.id + '&puid=' + puid + '&status=1" class="btn btn-danger">下架</a>';
            } else if (item.state == 1) {
                state = "已下架";
                button = '<a href="controller/Proxy_upstate.php?id=' + item.id + '&puid=' + puid + '&status=0" class="btn btn-primary">上架</a>';
            } else {
                state = "未知状态";
            }

            var orderLink = protocol + '//' + hostname + '/shop/links.php?goodsid=' + item.id + '&puid=' + puid;
            var orderButton = '<a href="' + orderLink + '" target="_blank" class="btn btn-warning">下单链接</a>';

            var html = '<tr>'+
                '<td>'+ item.id +'</td>'+
                '<td><img style="width:60px;" src="'+ item.zhutu +'"></td>'+ 
                '<td>'+ item.yys +'</td>'+ 
                '<td>'+ item.name +'</td>'+ 
                '<td>'+ item.intro +'</td>'+ 
                '<td>￥'+ item.price +'</td>'+
                 '<td>'+ item.gsd +'</td>'+ 
                '<td>'+ state +'</td>'+
                '<td>'+ item.notes +'</td>'+ 
                '<td>'+ button + orderButton +
                '<a class="btn btn-success" data-bs-effect="effect-scale" data-bs-toggle="modal" href="#modaldemo8" onclick="generateQRCode(\'' + orderLink + '\')">生成海报</a></td>'+
                '</tr>';
            $("#goods-body").append(html);
        }
    },

    error: function() {
        // 获取失败后的代码
        alert("获取商品信息失败");
    }
});

function generateQRCode(orderLink) {
    $.ajax({
        url: "controller/QRcode.php",
        type: "POST",
        data: {
            orderLink: orderLink
        },
        dataType: "json",  // 指定返回数据的类型为JSON
        success: function(data) {
            // data.qrCodeBase64 是二维码的base64编码
            $(".qr-code-image").attr("src", "data:image/png;base64," + data.qrCodeBase64);
        }
    });
}



    $(document).ready(function() {
        <?php
        if ($proxy_rename_sta == 0) {
            // 未实名认证
            echo '$("#modaldemo5").modal("show");';
            echo '$("#modaldemo5").modal({backdrop: "static", keyboard: false});';
        } elseif ($proxy_rename_sta == 1) {
            // 已实名认证
            echo '$("#modaldemo5").modal("hide");';
        }
        ?>
    });

function savePoster() {
  var canvas = document.createElement('canvas');
  var ctx = canvas.getContext('2d');

  // 获取当前显示的海报和二维码图像元素
  var activeCarouselItem = document.querySelector('.carousel-item.active');
  var posterImage = activeCarouselItem.querySelector('.poster-image');
  var qrCodeImage = activeCarouselItem.querySelector('.qr-code-image');

  // 获取海报图像的实际大小
  var posterWidth = posterImage.naturalWidth;
  var posterHeight = posterImage.naturalHeight;

  // 获取设备像素比
  var dpr = window.devicePixelRatio || 1;

  // 设置canvas的像素大小以匹配海报的实际大小，并增加像素密度
  var canvasWidth = posterWidth * dpr;
  var canvasHeight = posterHeight * dpr;
  canvas.width = canvasWidth;
  canvas.height = canvasHeight;

  // 缩放context以适应设备像素比
  ctx.scale(dpr, dpr);

  // 在canvas上绘制海报
  ctx.drawImage(posterImage, 0, 0, canvasWidth, canvasHeight);

  // 获取二维码图像的实际大小（不考虑像素密度）
  var qrCodeWidth = qrCodeImage.naturalWidth;
  var qrCodeHeight = qrCodeImage.naturalHeight;

  // 缩小二维码的大小
  var qrCodeScale = 0.5; // 调整这个比例以缩小二维码的大小
  qrCodeWidth *= qrCodeScale;
  qrCodeHeight *= qrCodeScale;

  // 计算二维码在canvas上的位置和大小，以适应高像素密度的绘制
  var qrCodeX = (posterWidth - qrCodeWidth) * dpr - 30; // 向左偏移20个像素
  var qrCodeY = (posterHeight - qrCodeHeight) * dpr - 30; // 向上偏移20个像素
  var qrCodeWidthScaled = qrCodeWidth * dpr;
  var qrCodeHeightScaled = qrCodeHeight * dpr;

  ctx.drawImage(
    qrCodeImage,
    qrCodeX,
    qrCodeY,
    qrCodeWidthScaled,
    qrCodeHeightScaled
  );

  // 创建一个新的图像文件并下载它
  var link = document.createElement('a');
  link.href = canvas.toDataURL('image/png');
  link.download = 'liuyueyanyan_powerby_gzking.png';
  link.click();
}


</script>

<?php
} else {
    // 未找到相关数据
    // echo '数据不存在';
}
?>
<script>
function openShopLink() {
    var url = "../shop/index.php?puid=" + puid;
    window.open(url, '_blank');
}
</script>

<?php require_once('common/footer.php'); ?>
 <?php
} else {
  echo "<script>alert('登录过过期，没有权限访问该页面');location.href='login.html';</script>";
}

